<?php


namespace app\api\controller;


use app\BaseController;
use app\middleware\Auth;
use app\api\traits\Category;
use think\middleware\AllowCrossDomain;

class Article extends BaseController
{
    use Category;
    protected $middleware = [AllowCrossDomain::class, Auth::class];
    protected $moduleid = 11;

    public function initialize()
    {
        parent::initialize();
    }

    public function index()
    {

    }

    public function add()
    {
        $post = $this->request->post();
        $post['catid'] = (int)$post['catid'];
        $post['addtime'] = time();
        $post['edittime'] = time();
        $post['userid'] = $this->request->userid;
        $post['username'] = $this->request->username;
        $post['editorid'] = $this->request->userid;
        $post['editor'] = $this->request->username;

        if (!$post['catid']) {
            return $this->msg(0, '所属分类不能为空');
        }
        if (!$post['title']) {
            return $this->msg(0, '标题不能为空');
        }
        if (!$post['content']) {
            return $this->msg(0, '内容不能为空');
        }
        if (!$post['flag']) {
            return $this->msg(0, '状态不能为空');
        }
        $insertid = \think\facade\Db::name('article')->insertGetId($post);
        return $this->msg($insertid);
    }

    public function mod()
    {
        $post = $this->request->post();
        $post['catid'] = intval($post['catid']);
        $post['itemid'] = intval($post['itemid']);
        $post['edittime'] = time();
        $post['editorid'] = $this->request->userid;
        $post['editor'] = $this->request->username;

        if (!$post['catid']) {
            return $this->msg(0, '所属分类不能为空');
        }
        if (!$post['title']) {
            return $this->msg(0, '标题不能为空');
        }
        if (!$post['content']) {
            return $this->msg(0, '内容不能为空');
        }
        if (!$post['flag']) {
            return $this->msg(0, '状态不能为空');
        }

        $r = \think\facade\Db::name('article')->where(['itemid' => $post['itemid']])->update($post);
        return $this->msg($r === false ? false : true);
    }

    public function preview()
    {
        $itemid = (int)$this->request->post('itemid', 0);
        $formated = (int)$this->request->post('formated', 0);
        $data = \think\facade\Db::name('article')->where(['itemid' => $itemid])->find();
        if (!$data) {
            return $this->msg(0);
        }
        if ($formated) {
            $data['addtime'] = date('Y-m-d H:i:s', $data['addtime']);
            $data['edittime'] = date('Y-m-d H:i:s', $data['edittime']);
            $data['flag'] = myswitch($data['flag'], $this->core_enums['flag']);
            $data['cat_pos'] = \think\facade\Db::name('category')->where(['catid' => $data['catid']])->value('cat_pos');
        }
        return $this->msg(1, '', '', $data);
    }

    public function lst()
    {
        $category_model = new \models\Category();
        $page = (int)$this->request->post('page', 1);
        $itemid = (int)$this->request->post('itemid', 0);
        $catid = (int)$this->request->post('catid', 0);
        $title = trim($this->request->post('title', ''));
        $flag = (int)$this->request->post('flag', 0);
        $is_export = (int)$this->request->post('is_export', 0);
        $filename = $this->request->post('filename', '');
        $where = [];
        if ($itemid) {
            $where[] = ['itemid', '=', $itemid];
        }
        if ($catid) {
            $where[] = ['catid', '=', $catid];
        }
        if ($title) {
            $where[] = ['title', 'like', "%{$title}%"];
        }
        if ($flag) {
            $where[] = ['flag', '=', $flag];
        }
        //导出Excel
        if ($is_export) {
            $pagesize = 20;
            $filename = $filename ? $filename : time() . rand(10000, 99999) . '.xlsx';
            $disk_path = root_path() . 'public/downloads/' . $filename;
            $browser_path = $this->request->domain() . '/downloads/' . $filename;

            $total = \think\facade\Db::name('article')->where($where)->count('*');
            $total_page = ceil($total / $pagesize);
            if ($total_page == 0) $total_page = 1;
            if ($page >= $total_page) {
                return $this->msg(1, '', '', [
                    'status' => true,
                    'total' => $total,
                    'page' => $page,
                    'total_page' => $total_page,
                    'progress' => round($page / $total_page, 2) * 100 . '%',
                    'filename' => $filename,
                    'browser_path' => $browser_path,
                ]);
            }
            $model = new \models\Article();
            $data = \think\facade\Db::name('article')->where($where)->order('itemid', 'desc')->page($page, $pagesize)->select()->toArray();
            $model->export($disk_path, $data);

            sleep(1);
            return $this->msg(1, '', '', [
                'status' => false,
                'total' => $total,
                'page' => $page,
                'total_page' => $total_page,
                'progress' => round($page / $total_page, 2) * 100 . '%',
                'filename' => $filename,
                'browser_path' => '',
            ]);
        }

        $rows = \think\facade\Db::name('article')->where($where)->order('itemid desc')->page($page, 20)->select()->toArray();
        $total = \think\facade\Db::name('article')->where($where)->count('*');
        foreach ($rows as $k => $v) {
            $rows[$k]['addtime'] = date('Y-m-d H:i:s', $v['addtime']);
            $rows[$k]['edittime'] = date('Y-m-d H:i:s', $v['edittime']);
            $rows[$k]['flag'] = myswitch($v['flag'], $this->core_enums['flag']);
            $rows[$k]['cat_pos'] = \think\facade\Db::name('category')->where(['catid' => $v['catid']])->value('cat_pos');
        }
        return $this->msg(1, '', '', [
            'rows' => $rows,
            'total' => $total,
        ]);
    }

    public function del()
    {
        $itemid = (int)$this->request->post('itemid', 0);
        $r = \think\facade\Db::name('article')->where(['itemid' => $itemid])->delete();
        return $this->msg($r);
    }

    public function batch_update()
    {
        $action = $this->request->post('action', '');
        $itemids = $this->request->post('itemids', []);
        switch ($action) {
            case 'catid':
                $catid = (int)$this->request->post('catid', 0);
                $r = \think\facade\Db::name('article')->where('itemid', 'in', $itemids)->update(['catid' => $catid]);
                return $this->msg($r === false ? false : true);
                break;
            case 'flag':
                $flag = (int)$this->request->post('flag', 0);
                $r = \think\facade\Db::name('article')->where('itemid', 'in', $itemids)->update(['flag' => $flag]);
                return $this->msg($r === false ? false : true);
                break;

        }
        return $this->msg(1);
    }
}